package com.hzqy.service.activity.interf;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

import com.hzqy.web.vo.activity.RequestVo;
import com.hzqy.web.vo.activity.ResponseVo;



public class GetFromIouIface {
	private static final Log LOG = LogFactory.getLog(GetFromIouIface.class);
	/**
	 * 分配用户idou 
	 * 根据传入的 请求信息到I豆接口，返回XML字符串
	 * @param requestVo
	 * @return
	 */
	public static ResponseVo getXmlStringRetIdou(RequestVo requestVo,Map map){
		String url = map.get("idouIP")+"/IDMall/hw/ticket/assignment.py?userid="+requestVo.getUserid()+"&siteid="+requestVo.getSiteid()+"&plid="+requestVo.getPlid()+"&count="+requestVo.getCount();
		LOG.debug("送I豆地址:"+url);
		ResponseVo result = getResultFromUrl(url);
		return result;
	}
	/**
	 * 获取用户idou信息
	 * @param requestVo
	 * @return
	 */
	private static ResponseVo getXmlStrGetUserIdouCount(RequestVo requestVo,Map map){
		String url = map.get("idouIP")+"/IDMall/hw/ibeans/index.py?userid="+requestVo.getUserid();
		ResponseVo result = getResultFromUrl(url);
		return result;
	}
	
	/**
	 * 请求 公共接口
	 * @param requestVo
	 * @return
	 */
	private static ResponseVo getResultFromUrl(String url){
		HttpURLConnection conn = null;
		String Result = null;
		ResponseVo res=null;
		InputStream is = null;
		try {
			URL url1 = new URL(url);
			conn = (HttpURLConnection) url1.openConnection();
			conn.setUseCaches(false);
			conn.setDoOutput(true);

			is = conn.getInputStream();
			int resp = 0;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			while((resp = is.read())!=-1) {
				bos.write(resp);
			}
			Result = new String(bos.toByteArray(),"gbk");
			LOG.debug("i豆返回："+Result);
			res= parseXMLStrForRetIdou(Result);
		} catch (IOException e) {
			LOG.error("i豆赠送失败",e);
		} finally {
			if (conn != null)
				conn.disconnect();
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
		return res;
	}
	/**
	 * 读取XML格式的字符串，根据属性来查找值
	 * 
	 * @param xmlStr是要解析的xml格式的字符串
	 * @param name是在xml字符串中要获得标签属性
	 * @return 返回的标签属性是name的值
	 */
	private static ResponseVo parseXMLStrForRetIdou(String xmlStr) {
		String value = null;
		StringReader sr = new StringReader(xmlStr);
		InputSource is = new InputSource(sr);
		SAXBuilder sb = new SAXBuilder();
		ResponseVo resVo = new ResponseVo();
		try {
			Document d = sb.build(is);
			Element e = d.getRootElement();
//			System.out.println("E--"+e.getChildText("retCode"));
		//	e = e.getChild("res");
			value = e.getChildText("retCode");//
			if (value == null) {
				return null;
			}  else {
				resVo.setRetCode(value);
			}
			value = e.getChildText("retMsg");
			if (value == null) {
				return null;
			} else{ 
				resVo.setRetMsg(value);
			} 
		} catch (JDOMException e) {
			e.printStackTrace();
			LOG.error("读取XML格式的字符串，根据属性来查找值出错：", e);
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error("读取XML格式的字符串，根据属性来查找值出错：", e);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("读取XML出错：", e);
		}
		return resVo;
	}
	
	//解析获取用户idou信息的xml
	private static ResponseVo parseXMLStrForGetUserIdou(String xmlStr) {
		String value = null;
		StringReader sr = new StringReader(xmlStr);
		InputSource is = new InputSource(sr);
		SAXBuilder sb = new SAXBuilder();
		ResponseVo resVo = new ResponseVo();
		try {
			Document d = sb.build(is);
			Element e = d.getRootElement();
//			System.out.println("E--"+e.getChildText("retCode"));
		//	e = e.getChild("res");
			value = e.getChildText("ibeans");// state 处理结果,'+OK'成功,'-ERR'失败
			if (value == null) {
				return null;
			}  else {
				resVo.setIbeans(value);
			}
			//limitfee  "YES", 充值已经达到上限; "NO", 充值未达到上限
		} catch (JDOMException e) {
			e.printStackTrace();
			LOG.error("读取XML格式的字符串，根据属性来查找值出错：", e);
		} catch (IOException e) {
			e.printStackTrace();
			LOG.error("读取XML格式的字符串，根据属性来查找值出错：", e);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error("读取XML出错：", e);
		}
		return resVo;
	}
	public static void main(String[] args){
		String xmlStr = null;
		RequestVo requestVo = new RequestVo();
		requestVo = new RequestVo();
		requestVo.setAction("1");
		requestVo.setCount(1);
		requestVo.setPlid(1);
		requestVo.setSiteid("1");
		requestVo.setUserid("1");
		Map<String, String>map=new HashMap<String, String>();
		map.put("idouIP", "http://60.191.98.23:8080");
		getXmlStringRetIdou(requestVo,map);
		ResponseVo res = parseXMLStrForRetIdou(xmlStr);
		System.out.println("ret code--"+res.getRetCode());
		System.out.println("ret msg"+res.getRetMsg());
		getXmlStrGetUserIdouCount(requestVo,map);
		ResponseVo res1 = parseXMLStrForGetUserIdou(xmlStr);
		System.out.println("ret ibeans-- -"+res1.getIbeans());
	}
	
}
